From e2b3cbac23a2cbc6e45d2a0aa401d8c2254e09b9 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 21 Oct 2020 23:54:09 -0400 Subject: [PATCH] colorbutton: Improve accessibility Make the swatch not appear selectable, and set an accessible label on it. --- gtk/gtkcolorbutton.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/gtk/gtkcolorbutton.c b/gtk/gtkcolorbutton.c index b3abdf626b..f4f8cc40fb 100644 --- a/gtk/gtkcolorbutton.c +++ b/gtk/gtkcolorbutton.c @@ -249,10 +249,13 @@ gtk_color_button_init (GtkColorButton *button) g_signal_connect (button->button, "clicked", G_CALLBACK (gtk_color_button_clicked), button); gtk_widget_set_parent (button->button, GTK_WIDGET (button)); - button->swatch = gtk_color_swatch_new (); + button->swatch = g_object_new (GTK_TYPE_COLOR_SWATCH, + "accessible-role", GTK_ACCESSIBLE_ROLE_IMG, + "selectable", FALSE, + "has-menu", FALSE, + NULL); gtk_widget_set_can_focus (button->swatch, FALSE); gtk_widget_remove_css_class (button->swatch, "activatable"); - g_object_set (button->swatch, "has-menu", FALSE, NULL); layout = gtk_widget_create_pango_layout (GTK_WIDGET (button), "Black"); pango_layout_get_pixel_extents (layout, NULL, &rect); g_object_unref (layout); @@ -437,11 +440,37 @@ gtk_color_button_clicked (GtkButton *b, gtk_window_present (GTK_WINDOW (button->cs_dialog)); } +static guint +scale_round (double value, + double scale) +{ + value = floor (value * scale + 0.5); + value = CLAMP (value, 0, scale); + return (guint)value; +} + +static char * +accessible_color_name (const GdkRGBA *color) +{ + if (color->alpha < 1.0) + return g_strdup_printf (_("Red %d%%, Green %d%%, Blue %d%%, Alpha %d%%"), + scale_round (color->red, 100), + scale_round (color->green, 100), + scale_round (color->blue, 100), + scale_round (color->alpha, 100)); + else + return g_strdup_printf (_("Red %d%%, Green %d%%, Blue %d%%"), + scale_round (color->red, 100), + scale_round (color->green, 100), + scale_round (color->blue, 100)); +} + static void gtk_color_button_set_rgba (GtkColorChooser *chooser, const GdkRGBA *rgba) { GtkColorButton *button = GTK_COLOR_BUTTON (chooser); + char *text; g_return_if_fail (GTK_IS_COLOR_BUTTON (chooser)); g_return_if_fail (rgba != NULL); @@ -449,6 +478,12 @@ gtk_color_button_set_rgba (GtkColorChooser *chooser, button->rgba = *rgba; gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (button->swatch), &button->rgba); + text = accessible_color_name (rgba); + gtk_accessible_update_property (GTK_ACCESSIBLE (button->swatch), + GTK_ACCESSIBLE_PROPERTY_LABEL, text, + -1); + g_free (text); + g_object_notify (G_OBJECT (chooser), "rgba"); } -- 2.30.2